package p.ithorns.framework.feign.enhance;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * FeignSvcJkdProxy
 * TODO ithorns
 *
 * @author Ht.L
 * @date 2024-03-29 15:22
 * @since 1.0.0
 */
public class FeignSvcJdkProxy implements InvocationHandler {

    private final Object target;

    public FeignSvcJdkProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if ("equals".equals(method.getName()) && method.getParameterCount() == 1) {
            try {
                final Object otherHandler =
                        args.length > 0 && args[0] != null ? Proxy.getInvocationHandler(args[0]) : null;
                return equals(otherHandler);
            } catch (final IllegalArgumentException e) {
                return false;
            }
        } else if ("hashCode".equals(method.getName()) && method.getParameterCount() == 0) {
            return hashCode();
        } else if ("toString".equals(method.getName()) && method.getParameterCount() == 0) {
            return toString();
        }
        return method.invoke(target, args);
    }
}